﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AndroidApp1
{
    public static class IOTool
    {
        const string indexpath = "https://gitee.com/lightsever/thinapi/raw/master/webtool/ttpixel/packindex.json";
        public static string GetDownloadIndexFile()
        {
            return indexpath;
        }
        public static string GetDownloadIndexPath()
        {
            var urllast = indexpath.Substring(0, indexpath.LastIndexOf('/') + 1);
            return urllast;
        }
        public static string GetHotDataPath()
        {
            var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            var loadingpath = System.IO.Path.Combine(path, "hotdata");
            return loadingpath;
        }

        public static string GetDownURL(string filename)
        {
            var urllast = indexpath.Substring(0, indexpath.LastIndexOf('/') + 1);



            return urllast + filename.Replace("\\", "/"); ;
        }
        public static void WriteHotFile(byte[] src, string filename, string hash)
        {
            var path = System.IO.Path.Combine(GetHotDataPath(), filename.Replace("\\", "/"));
            var dir = System.IO.Path.GetDirectoryName(path);
            if (!System.IO.Directory.Exists(dir))
                System.IO.Directory.CreateDirectory(dir);
            if (path.Contains(".html_"))
                path = path.Replace(".html_", ".html");

            System.IO.File.WriteAllBytes(path, src);
            System.IO.File.WriteAllText(path + ".hash", hash);

        }
        public static string GetHash(string filename)
        {
            var path = System.IO.Path.Combine(GetHotDataPath(), filename.Replace("\\", "/") + ".hash");

            if (path.Contains(".html_"))
                path = path.Replace(".html_", ".html");

            if (System.IO.File.Exists(path) == false)
                return null;

            var pathhash = System.IO.File.ReadAllText(path);
            return pathhash;
        }

    }
}
